package com.js.data.system.util;

import cn.hutool.extra.spring.SpringUtil;
import com.js.data.common.utils.SecurityUtils;
import com.js.data.system.entity.SysUser;
import com.js.data.system.service.DeptService;
import com.js.data.system.service.UserService;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.List;

/**
 * @author admin
 */
public class UserUtil {
    private static final UserService userService = SpringUtil.getBean(UserService.class);
    private static final DeptService deptService = SpringUtil.getBean(DeptService.class);
    public static Long getUserId() {
        try {
            return SecurityUtils.getCurrentUserId();
        } catch (Exception e) {
            return 0L;
        }
    }

    public static UserDetails getUser() {
        try {
            return SecurityUtils.getCurrentUser();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取用户数据权限 部门id列表
     *
     * @return List<Long> 空是本人 null是全部，其他按机构计算
     * @author hh
     * @since 1.0.0
     */
    public static List<Long> getAuthOrgIdList() {
        List<Long> retList = new ArrayList<>();
        SysUser user = userService.getById(getUserId());
        switch (user.getPermType()) {
            // 全部
            case "0":
                retList = null;
                break;
            // 本人
            case "1":
                break;
            // 本机构
            case "2":
                retList.add(user.getDeptId());
                break;
            // 本机构及其下级
            case "3":
                retList= deptService.listAllChildDeptIdAndSelf(user.getDeptId());
                break;
            // 指定
            case "4":
                //指定机构
                //retList = userPermOrgList.stream().map(SysUserPermOrg::getOrgId).collect(Collectors.toList());
                break;
            default:
                break;
        }
        return retList;
    }
    public static void main(String [] arg){
        String ss = "abcdefghi";
        System.out.println(ss);
        System.out.println(ss.substring(0,3));
        System.out.println(ss.substring(3,5));
        System.out.println(ss.substring(5,8));
        System.out.println(ss.substring(8));
    }
}
